home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PASCTXT.ZIP / CHAP6.TXT < prev    next >
Text File  |  1988-01-15  |  20KB  |  455 lines

  1.  
  2.             CHAPTER 6 - Arrays, types, constants, and labels
  3.  
  4.  
  5.                                   ARRAYS
  6.  
  7.              At  the  beginning  of this tutorial  we  said  that  a
  8.         computer   program  is  composed  of  data  and   executable
  9.         statements  to do something with that data.  Having  covered
  10.         nearly  all  of the programming statements, we must  now  go
  11.         back  and fill in some gaps in our data definition and  look
  12.         at the array in particular.
  13.  
  14.              One  of the most useful Pascal data structures  is  the
  15.         array, which is, in the simplest terms, a group of 2 or more
  16.         identical terms, all having the same type.  Lets go directly
  17.         to  an example to see what an array looks like. Display  the
  18.         Pascal  program ARRAYS and notice line 5 starting  with  the
  19.         word Automobiles.  The variable Automobiles is defined as an
  20.         integer  variable  but in addition, it is  defined  to  have
  21.         twelve  different integer variables,  namely  Automobile[1],
  22.         Automobile[2], Automobile[3], .. Automobile[12].
  23.  
  24.              The  square  braces  are used in  Pascal  to  denote  a
  25.         subscript for an array variable.  The array definition given
  26.         in line 5 is the standard definition for an array, namely  a
  27.         variable  name  followed by a colon and  the  reserved  word
  28.         "array",  with  the  range  of the  array  given  in  square
  29.         brackets followed by another reserved word "of" and  finally
  30.         the type of variable for each element of the array.
  31.  
  32.              In  using the elements of the array in a program,  each
  33.         of  the  elements of the array are required to  be  used  in
  34.         exactly  the same manner as any simple variable  having  the
  35.         same type.  Each time one of the variables is used, it  must
  36.         have  the subscript since the subscript is now part  of  the
  37.         variable name.  The subscript moreover, must be of the  type
  38.         used  in  the  definition and it must be  within  the  range
  39.         defined or it will be listed as an error.
  40.  
  41.              Now  consider the program itself.  As Index  is  varied
  42.         from  1 to 12, the range of the variable Automobile, the  12
  43.         variables  are  set to the series of values 11 to  22.   Any
  44.         integer values could be used, this was only a convenient way
  45.         to  set the values to some well defined numbers.   With  the
  46.         values  stored,  a  header is now printed and  the  list  of
  47.         values  contained in the array is printed.   Note  carefully
  48.         that,  although the subscripts are limited to 1 through  12,
  49.         the  values stored in each of the 12 variables  are  limited
  50.         only  by  the  range of integers, namely  -32768  to  32767.
  51.         Review  this material and this program as long as needed  to
  52.         fully understand it, as it is very important.
  53.  
  54.              Keep in mind that the array is actually composed of  12
  55.         different integer type variables that can be used in any way
  56.  
  57.  
  58.                                 Page 36
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.             CHAPTER 6 - Arrays, types, constants, and labels
  69.  
  70.  
  71.         that  it  is legal to use any other integer  type  variable.
  72.         Compile and run this program.
  73.  
  74.                            DOUBLY INDEXED ARRAYS
  75.  
  76.              After understanding the above, load the program ARRAYS2
  77.         to see the next level of complexity of arrays.  You will see
  78.         that  Checkerboard is defined as an array from 1 to  8,  but
  79.         instead of it being a simple data type, it is itself another
  80.         array   from  1  to  8  of  type  integer.    The   variable
  81.         Checkerboard  is  actually composed of 8 elements,  each  of
  82.         which is 8 elements, leading to a total of 64 elements, each
  83.         of  which  is a simple integer variable.  This is  called  a
  84.         doubly subscripted array and it can be envisioned in exactly
  85.         the  same manner as a real checker board, an 8 by 8  matrix.
  86.         Another way to achieve the same end is to define the  double
  87.         array  as  in the next line of the program  where  Value  is
  88.         defined as a total of 64 elements.
  89.  
  90.              To  use  either of the two variables in a  program,  we
  91.         must  add  two subscripts to the variable name to  tell  the
  92.         program which element of the 64 we desire to use.  Examining
  93.         the  program  will reveal two loops, one nested  within  the
  94.         other, and both ranging in value from 1 to 8.  The two  loop
  95.         indices  can therefore be used as subscripts of the  defined
  96.         array  variables.  The variable Checkerboard is  subscripted
  97.         by both of the loop indices and each of the 64 variables  is
  98.         assigned a value as a function of the indices.  The assigned
  99.         value  has no real meaning other than to illustrate  to  you
  100.         how  it  is done.  Since the value of  Checkerboard  is  now
  101.         available,  it is used to define some values to be used  for
  102.         the variable Value in line 12 of the program.
  103.  
  104.              After  defining all of those variables, and you  should
  105.         understand that we have defined a total of 128 variables  in
  106.         the  double loop, 64 of Checkerboard and 64 of  Value,  they
  107.         can  be printed out.  The next section of the  program  does
  108.         just that, by using another doubly nested loop, with a Write
  109.         statement in the center.  Each time we go through the center
  110.         of the loop we tell it to print out one of the 64  variables
  111.         in the Checkerboard matrix with the indices Index and  Count
  112.         defining which of the variables to write each time.  Careful
  113.         study of the loop should reveal its exact operation.
  114.  
  115.              After  printing out the matrix defined by the  variable
  116.         Checkerboard  we  still  have  the  matrix  defined  by  the
  117.         variable  Value  intact  (In  fact, we  still  have  all  of
  118.         Checkerboard  available  because we haven't changed  any  of
  119.         it).  Before printing out the matrix defined by Value, let's
  120.         change  a  few of the elements just to see how it  is  done.
  121.         The  code  in  lines 24 to 26 simply  change  three  of  the
  122.  
  123.  
  124.                                 Page 37
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.             CHAPTER 6 - Arrays, types, constants, and labels
  135.  
  136.  
  137.         variables  to illustrate that you can operate on all of  the
  138.         matrix  in  loops, or on any part of the  matrix  in  simple
  139.         assignment statements.  Notice especially line 26, in  which
  140.         "Value[3,6]" (which was just set to the value of 3), is used
  141.         as a subscript.  This is perfectly legal since it is defined
  142.         as a simple integer variable and is within the range of 1 to
  143.         8, which is the requirement for a subscript of the  variable
  144.         Value.   The last part of the program simply prints out  the
  145.         64  values  of  the variable Value  in the  same  manner  as
  146.         above.   Notice  that when you run the  program,  the  three
  147.         values are in fact changed as expected.
  148.  
  149.                             ARRAYS ARE FLEXIBLE
  150.  
  151.              A  few  more words about arrays before we go  on.   The
  152.         arrays  in the last program were both defined to be  square,
  153.         namely  8 by 8, but that choice was purely  arbitrary.   The
  154.         subscripts were chosen to go from 1 to 8 but they could have
  155.         been chosen to go from 101 to 108 or any other range  needed
  156.         to clearly define the problem at hand.  And, as you may have
  157.         guessed, you are not limited to a doubly subscripted  matrix
  158.         but you can define a variable with as many subscripts as you
  159.         need  to  achieve your desired end.  There  is  a  practical
  160.         limit  to  the  number of subscripts because  you  can  very
  161.         quickly  use up all of your available memory with one  large
  162.         subscripted variable.
  163.  
  164.                             THE TYPE DEFINITION
  165.  
  166.              Now  that  you understand arrays, lets look at  a  more
  167.         convenient  way to define them by examining the Pascal  file
  168.         TYPES.   You will notice a new section at the  beginning  of
  169.         the  listing  with  the heading type.  The  word  "type"  is
  170.         another  reserved word which is used at the beginning  of  a
  171.         section used to define "user-defined types".  Beginning with
  172.         the simple predefined types we studied earlier, we can build
  173.         up  as many new types as we need and they can be as  complex
  174.         as we desire.  The six names (from Array_Def to Boat) in the
  175.         type section are not variables, but are defined to be  types
  176.         and  can  be used in the same manner as can  integer,  byte,
  177.         real, etc.
  178.  
  179.              This is a very difficult concept, but a very  important
  180.         one.   The Pascal compiler is very picky about the  variable
  181.         types  you  use in the program, doing lots  of  checking  to
  182.         insure  that you do not use the wrong type anywhere  in  the
  183.         program.   Because  it is picky, you could  do  very  little
  184.         without  the  ability to define new types when  needed,  and
  185.         that  is the reason Pascal gives you the ability  to  define
  186.         new types to solve a particular problem.
  187.  
  188.  
  189.  
  190.                                 Page 38
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.             CHAPTER 6 - Arrays, types, constants, and labels
  201.  
  202.  
  203.              Some  of  these types are used in the  var  declaration
  204.         part of the program.  Notice that since Airplane is an array
  205.         of  Dog_Food  and Dog_Food is in turn an array  of  boolean,
  206.         then  Airplane  defines  a doubly  subscripted  array,  each
  207.         element being a boolean variable.  This does not define  any
  208.         variables, only a user defined type, which can be used in  a
  209.         var  to  define a matrix of boolean variables.  This  is  in
  210.         fact  done in the definition of Puppies, which is  an  array
  211.         composed of 72 (6 times 12) boolean variables.  In the  same
  212.         manner, Stuff is composed of an array of 14 variables,  each
  213.         being  an integer variable.  The elements of the array  are,
  214.         Stuff[12], Stuff[13], .. Stuff[25].  Notice also that Stuff2
  215.         is  also  defined  in exactly the same manner  and  is  also
  216.         composed of 14 variables.
  217.  
  218.              Careful  inspection  will  reveal  that  Kitties  is  a
  219.         variable which has the same definition as Puppies.  It would
  220.         probably  be  poor programming practice to  define  them  in
  221.         different   manners  unless  they  were  in   fact   totally
  222.         disassociated.   In  this  example  program,  it  serves  to
  223.         illustrate  some  of  the ways  user-defined  types  can  be
  224.         defined.
  225.  
  226.              Be sure to compile and run this program.
  227.  
  228.                     IS THE CONCEPT OF "TYPES" IMPORTANT?
  229.  
  230.              If you spend the time to carefully select the types for
  231.         the variables used in the program, the Pascal compiler  will
  232.         do some debugging for you since it is picky about the use of
  233.         variables with different types.  Any aid you can use to help
  234.         find  and remove errors from your program is useful and  you
  235.         should  learn to take advantage of type checking.  The  type
  236.         checking in Pascal is relatively weak compared to some other
  237.         languages such as Modula-2 or Ada, but still very useful.
  238.  
  239.              In  a tiny program like this example, the value of  the
  240.         type declaration part cannot be appreciated, but in a  large
  241.         program  with  many variables, the type declaration  can  be
  242.         used to great advantage.  This will be illustrated later.
  243.  
  244.                          THE CONSTANT DECLARATION
  245.  
  246.              Examining the Pascal example program CONSTANT will give
  247.         us  an example of a constant definition.  The reserved  word
  248.         "const"  is  the beginning of the section that  is  used  to
  249.         define constants that can be used anyplace in the program as
  250.         long  as they are consistent with the required  data  typing
  251.         limitations.   In  this example, Max_Size is  defined  as  a
  252.         constant  with the value of 12. This is not a  variable  and
  253.         cannot  be  changed  in the program, but  is  still  a  very
  254.  
  255.  
  256.                                 Page 39
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.             CHAPTER 6 - Arrays, types, constants, and labels
  267.  
  268.  
  269.         valuable  number.   For  the  moment  ignore  the  next  two
  270.         constant definitions.  As we inspect the type  declarations,
  271.         we  see two user-defined types, both of which are arrays  of
  272.         size 1 to 12 since Max_Size is defined as 12.  Then when  we
  273.         get  to  the var declaration part, we  find  five  different
  274.         variables, all defined as arrays from 1 to 12 (some are type
  275.         integer  and  some  are type char).  When  we  come  to  the
  276.         program we find that it is one big loop which we go  through
  277.         12 times because the loop is executed Max_Size times.
  278.  
  279.              In the above definition, there seems to be no advantage
  280.         to  using  the constant, and there is none, until  you  find
  281.         that  for some reason you wish to increase the range of  all
  282.         arrays  from 12 to 18.  In order to do so, you only need  to
  283.         redefine the value of the constant, recompile, and the whole
  284.         job  is  done.  Without the constant definition,  you  would
  285.         have had to change all type declarations and the upper limit
  286.         of the loop in the program.  Of course that would not be too
  287.         bad  in the small example program, but could be a real  mess
  288.         in  a 2000 line program, especially if you  missed  changing
  289.         one  of the 12's to an 18. That would be a good  example  of
  290.         data  in  and garbage out.  This program should give  you  a
  291.         good  idea of what the constant can be used for, and as  you
  292.         develop  good  programming  techniques,  you  will  use  the
  293.         constant declaration to your advantage.
  294.  
  295.                       THE TURBO PASCAL TYPED CONSTANT
  296.  
  297.              We   skipped  over  the  second  and   third   constant
  298.         declaration  for a very good reason, they are  not  constant
  299.         declarations.   TURBO Pascal has defined, as  an  extension,
  300.         the  "typed constant".  Using the syntax shown,  Index_Start
  301.         is defined as an integer type variable and is initialized to
  302.         the value of 49.  This is a true variable and can be used as
  303.         such  in  the program.  The same effect can be  achieved  by
  304.         simply  defining Index_Start as an integer type variable  in
  305.         the  var declaration part and setting it to the value of  49
  306.         in  the  program itself.  Since it does not really  fit  the
  307.         definition of a constant, it's use is discouraged until  you
  308.         gain experience as a Pascal programmer.  Until then it  will
  309.         probably  only  be  confusing  to  you.   In  like   manner,
  310.         Check_It_Out  is a boolean type variable initialized to  the
  311.         value TRUE.  It is not a constant.
  312.  
  313.              The typed constants defined in the last paragraph  have
  314.         one  additional  characteristic, they are  initialized  only
  315.         once,  when  the  program is loaded.  Even when  used  in  a
  316.         procedure  or function, they are only initialized  when  the
  317.         program  is loaded, not upon each call to the  procedure  or
  318.         function.   Don't worry too much about this at  this  point,
  319.  
  320.  
  321.  
  322.                                 Page 40
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.             CHAPTER 6 - Arrays, types, constants, and labels
  333.  
  334.  
  335.         when  you gain experience with Pascal, you will be  able  to
  336.         use this information very effectively.
  337.  
  338.                            THE LABEL DECLARATION
  339.  
  340.              Finally, the example program LABELS will illustrate the
  341.         use  of  labels.   In the Pascal definition, a  label  is  a
  342.         number from 0 to 9999 that is used to define a point in  the
  343.         program  to  which  you wish to jump.  All  labels  must  be
  344.         defined  in the label definition part of the program  before
  345.         they  can be used.  Then a new reserved word "goto" is  used
  346.         to  jump to that point in the program.  The best way to  see
  347.         how  the goto is used with labels is to examine the  program
  348.         before you.
  349.  
  350.              TURBO  Pascal has an extension for labels.   Any  valid
  351.         identifier,  such  as used for variables, can be used  as  a
  352.         label  in addition to the values from 0 to 9999.  These  are
  353.         illustrated in the example program.
  354.  
  355.              When you compile and run this program, the output  will
  356.         look a little better than the program did.
  357.  
  358.                              THE PACKED ARRAY
  359.  
  360.              When  Pascal  was first defined in 1971,  many  of  the
  361.         computers in use at that time used very large words, 60 bits
  362.         being  a typical word size.  Memory was very  expensive,  so
  363.         large  memories were not too common.  A Pascal program  that
  364.         used  arrays was inefficient because only one  variable  was
  365.         stored  in  each word.  Most of the bits in each  word  were
  366.         totally  wasted,  so the packed array was defined  in  which
  367.         several  variables  were stored in each  word.   This  saved
  368.         storage space but took extra time to unpack each word to use
  369.         the data.  The programmer was given a choice of using a fast
  370.         scheme  that  wasted memory, the array, or a  slower  scheme
  371.         that used memory more efficiently, the packed array.
  372.  
  373.             The modern microcomputer has the best of both schemes, a
  374.         short word, usually 16 bits, and a large memory.  The packed
  375.         array  is therefore not even implemented in  many  compilers
  376.         and will be ignored during compilation.  The packed array is
  377.         specifically ignored by either TURBO Pascal compiler.
  378.  
  379.                       ONE MORE TURBO PASCAL EXTENSION
  380.  
  381.              Standard Pascal, as defined by Nicklaus Wirth, requires
  382.         that  the  various  fields in the  definition  part  of  the
  383.         program  come in a specific order and each must appear  only
  384.         once.   The specific order is, label, const, type, var,  and
  385.         finally the procedures and functions.  Of course, if any are
  386.  
  387.  
  388.                                 Page 41
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.             CHAPTER 6 - Arrays, types, constants, and labels
  399.  
  400.  
  401.         not needed, they are simply omitted.  This is a rather rigid
  402.         requirement   but  it  was  required  by  the  pure   Pascal
  403.         definition probably to teach good programming techniques  to
  404.         beginning students.
  405.  
  406.              All versions of TURBO Pascal are not nearly as rigid as
  407.         the  standard Pascal requirement.  You are permitted to  use
  408.         the  fields in any order and as often as you  wish  provided
  409.         that  you define everything before you use it, which is  the
  410.         unbroken rule of Pascal.  It sometimes makes sense to define
  411.         a few variables immediately after their types are defined to
  412.         keep  them  near their type definitions, then define  a  few
  413.         more types with the variables that are associated with  them
  414.         also.   TURBO Pascal gives you this extra  flexibility  that
  415.         can be used to your advantage.
  416.  
  417.  
  418.                            PROGRAMMING EXERCISES
  419.  
  420.         1.  Write  a program to store the integers 201 to 212 in  an
  421.             array then display them on the monitor.
  422.  
  423.         2.  Write a program to store a 10 by 10 array containing the
  424.             products  of  the indices,  therefore  a  multiplication
  425.             table. Display the matrix on the video monitor.
  426.  
  427.         3.  Modify  the program in 2 above to include a constant  so
  428.             that  by simply changing the constant,  the size of  the
  429.             matrix and the range of the table will be changed.
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                 Page 42
  455.